package com.agranee.timesheet.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.agranee.timesheet.daoable.IEmployee;
import com.agranee.timesheet.model.Employee;


@Transactional
public class EmployeeDaoImpl extends HibernateDaoSupport implements IEmployee  {
	
	@SuppressWarnings("unchecked")
	@Override
	
	public Employee getEmployeeByUserId(String userId) {		
		List<Employee> employeeList=getSessionFactory().getCurrentSession().createQuery("from Employee e where e.employeeUserId=?").setParameter(0, userId).list();
		//List<Employee> employeeList=getHibernateTemplate().find("from Employee e where e.employeeUserId=?", 1);
		return employeeList.get(0);
		
	}


	//@Transactional
	public Employee getUserByEmployeeId(String employeeId) {
		return  (Employee) getSessionFactory().getCurrentSession().createQuery("from Employee e where e.employeeId = ?").setParameter(0, employeeId).list().get(0);
		
	}



	@Override
	//@Transactional
	public void resetPassword(String employeeId,String newPassword) {
		
		Session session=this.getSessionFactory().getCurrentSession();
		Employee employee=(Employee) session.createQuery("from Employee e where e.employeeId = ?").setParameter(0, employeeId).list().get(0);
		employee.setEmployeePassword(newPassword);
		session.update(employee);
	}

	@Override
	public Employee getEmployeeByEmployeeId(String employeeId) {
		Session session=this.getSessionFactory().getCurrentSession();
		Employee employee=null;
		employee=(Employee)session.createQuery("from Employee e where e.employeeId = ?").setParameter(0, employeeId).list().get(0);
		
		return employee;
	}


}
